package com.kc.controller;

import com.kc.entity.Teacher;
import com.kc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jpademo")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 查询表中所有数据
     * */
    @GetMapping("/findall")
    public List<Teacher> findall(){
        List<Teacher> list = userService.findall();
        list.forEach(System.out::println);
        return list;
    }

    /**
     * 根据id查询数据
     * */
    @GetMapping("/finduser/{id}")
    public Teacher finduser(@PathVariable Integer id){
        Teacher teacher = userService.finduser(id);
        return teacher;
    }

    /**
     * 添加数据
     * */
    @PostMapping("/adduser/{id}/{age}/{name}")
    public List<Teacher> adduser(@PathVariable Integer id, @PathVariable String age,
                                 @PathVariable String name){
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setAge(age);
        teacher.setName(name);
        userService.adduser(teacher);
        List<Teacher> list = userService.findall();
        list.forEach(System.out::println);
        return list;
    }

    /**
     * 删除数据
     * */
    @DeleteMapping("/delete/{id}")
    public List<Teacher> deleteuser(@PathVariable Integer id){
        userService.removeuser(id);
        // 显示
        List<Teacher> list = userService.findall();
        list.forEach(System.out::println);
        return list;
    }

    /**
     * 修改数据
     * */
    @PostMapping("/update/{id}/{age}/{name}")
    public List<Teacher> updateuaser(@PathVariable Integer id, @PathVariable String age,
                                     @PathVariable String name){
        Teacher teacher = userService.finduser(id);
        teacher.setAge(age);
        teacher.setName(name);
        userService.updateuser(teacher);
        // 显示
        List<Teacher> list = userService.findall();
        list.forEach(System.out::println);
        return list;
    }

}
